//index.js
//获取应用实例
const app = getApp()
import { queryShopData, queryCartDetail , addCart } from '../../http/api.js'
Page({
  data: {
    selectCateId: 0,//当前分类id
    toView: '',//当前分类
    tabIndex: 0,//点菜0、商家1
    businessId: 0,//店铺ID
    shopInfo:{},//店铺信息
    cartList:[],//分类列表
    cartInfo:{},//购物车信息
    showCart:false,//是否展示购物车
    selectGG:[],//当前规格
    currentGG:'',//当前商品
    showGG:false//是否显示规格弹窗
  },
  onLoad: function (options) {
    this.setData({
      businessId: options.businessId
    });
    this.queryShopData();
    this.queryCartList();
  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  //根据商家id查询购物车
  queryCartList:function(){
    let businessId = this.data.businessId;
    const vm = this;
    queryCartDetail(businessId).then(res=>{
      if(res.code == 200){
        let cartList = res.data.cartGoodsDetails;
        if(!cartList || cartList.length == 0){
          this.setData({
            showCart: false
          });
        }
        let cartInfo = res.data;
        vm.setData({
          cartList,
          cartInfo
        })
      }
    });
  },
  //拉取店铺信息
  queryShopData: function(){
    //店铺id
    let businessId = this.data.businessId;
    const vm = this;
    queryShopData(businessId).then(res=>{
      vm.setData({
        shopInfo: res.data,
        selectCateId: res.data.categoryDetails[0].categoryId
      });
      wx.setNavigationBarTitle({title:res.data.businessName});
      // 给右侧商品列表循环赋值列表项的上边界坐标和高度
      vm.data.shopInfo.categoryDetails.forEach(item => {
        // 添加节点的布局位置的查询请求
        wx.createSelectorQuery().select('#productItem' + item.categoryId).boundingClientRect(rect => {
          item.offsetTop = rect.top;
          item.height = rect.height;
        }).exec()
      })
    });
  },
  //展示购物车
  showShopingCart: function(){
    this.setData({
      showCart :!this.data.showCart
    })
  },
  //隐藏购物车
  hidenShopingCart: function(){
    this.setData({
      showCart :false
    })
  },
  //提交订单
  goSubmit: function(){
    let businessId = this.data.businessId;
    wx.navigateTo({
      url: `/pages/submit/index?businessId=${businessId}`,
    })
  },
  //点击菜单处理
  handleMenu: function (e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      selectCateId: id,
      toView: 'productItem' + id
    });
  },
  // 监听右边商品列表滑动
  scrollProductList (e) {
    this.data.shopInfo.categoryDetails.forEach(item => {
      if (e.detail.scrollTop >= (item.offsetTop - 150 ) && e.detail.scrollTop <= (item.offsetTop - 150) + item.height) {
        this.setData({
          selectCateId: item.categoryId
        })
      }
    })
  },
  //购物车加减商品
  addShopingCar: function(e){
    let type = e.currentTarget.dataset.type;
    let item = e.currentTarget.dataset.item;
    let vm = this;
    let obj = {
      businessId: this.data.businessId,
      goodsDistPrice: item.goodsDistPrice,
      goodsDistStatus: item.goodsDistStatus,
      goodsId: item.goodsId,
      goodsImage: item.goodsImage,
      goodsPrice: item.goodsPrice
    };
    if(item.goodsSpecStr){
      obj['goodsSpecStr'] = item.goodsSpecStr;
    }else if(item.goodsSpecs && item.goodsSpecs.length > 0){
      obj['goodsSpecStr'] = this.data.selectGG.join(',');
    }
    addCart(type,obj).then(res=>{
      if(res.code == 200){
         vm.queryCartList();
      }
    });
   
  },
  //点击选规格按钮
  checkGG: function(e){
    let currentGG = e.currentTarget.dataset.item;//当前商品
    let goodsSpecs = currentGG.goodsSpecs;//规格列表
    let selectGG = [];//当前规格
    goodsSpecs.forEach(element => {
      selectGG.push(element.goodsSpecNameParam[0]);
    });
    this.setData({currentGG,selectGG,showGG:true});
  },
  //关闭规格弹窗
  colseGG: function(){
    this.setData({showGG:false});
  },
  //点击具体规格
  handleGG: function(e){
    let item = e.currentTarget.dataset.item;//当前商品
    let idx = e.currentTarget.dataset.idx;//当前商品
    let selectGG = this.data.selectGG;
    selectGG[idx] = item;
    console.log('当前规格：'+JSON.stringify(selectGG));
    this.setData({selectGG});
  }
})
